# -*- coding: utf-8 -*-
import csv
import os

from django.conf import settings
from django.test import TestCase, Client

# Create your tests here.
from demo.demo_access_mgmt.models import User


class BaseApiTestCase(TestCase):
    # 实例化 client 对象
    token = "123456"
    client = Client()
    # 给tb_user表里插入一条数据
    user_name = 'test-test'
    user_id = 'xxxxx'
    user = User()
    user.user_id = user_id
    user.status = '512'
    user.user_name = user_name
    user.save()
    # 调/user/info 模拟登录
    path = '/api/access/user/info/'
    resp = client.get(path, HTTP_AUTHORIZATION=token)

    # 判断单元测试报告文件是否存在，如果不存在进行创建
    if not os.path.exists(settings.BASE_LOG_DIR):
        os.makedirs(settings.BASE_LOG_DIR, exist_ok=True)
    file_name = f'{settings.BASE_TEST_DIR}/单元测试报告.csv'
    if not os.path.exists(file_name):
        f = open(file_name, "w", encoding="utf-8", newline='')
        # 创建csv写入对象
        writer = csv.writer(f)
        columns = ['模块名', '方法名', '中文描述', '输入', '输出', '状态', '测试阶段']
        writer.writerow(columns)
        f.close()

    # 清除测试数据
    User.objects.filter(user_id=user_id, user_name=user_name).delete()

    def setUp(self) -> None:
        # 这个方法会在每一个测试方法执行之前被调用，通常用来做一些准备工作
        self.f = open(self.file_name, "a+", encoding="utf-8", newline='')
        # 创建csv写入对象
        self.writer = csv.writer(self.f)
        print('1' * 100)

    def tearDown(self) -> None:
        # 这个方法会在每一个测试用法执行之后被被调用，通常用来做一些清理工作
        self.f.close()
        print('2' * 100)

    def get(self, path, model_name, function_name, desc, input_data, stage):
        resp = self.client.get(path, data=input_data, HTTP_AUTHORIZATION=self.token)
        result = resp.json()
        status = result['status']
        row = [model_name, function_name, desc, input_data, result, status, stage]
        self.writer.writerow(row)

    def post(self, path, model_name, function_name, desc, input_data, stage):
        resp = self.client.post(path, data=input_data, content_type="application/json; charset=utf-8",
                                HTTP_AUTHORIZATION=self.token)
        result = resp.json()
        status = result['status']
        row = [model_name, function_name, desc, input_data, result, status, stage]
        self.writer.writerow(row)
        return result

    def put(self, path, model_name, function_name, desc, input_data, stage):
        resp = self.client.put(path, data=input_data, content_type="application/json; charset=utf-8",
                               HTTP_AUTHORIZATION=self.token)
        result = resp.json()
        status = result['status']
        row = [model_name, function_name, desc, input_data, result, status, stage]
        self.writer.writerow(row)

    def delete(self, path, model_name, function_name, desc, input_data, stage):
        resp = self.client.delete(path, data=input_data, content_type="application/json; charset=utf-8",
                                  HTTP_AUTHORIZATION=self.token)
        result = resp.json()
        status = result['status']
        row = [model_name, function_name, desc, input_data, result, status, stage]
        self.writer.writerow(row)
